﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.Cells;
using JNPF.Common.Security;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.Logging.Attributes;
using JNPF.Website.Entitys;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using Spire.Presentation;
using SqlSugar;
using Website.Entitys.Dto;

namespace Website
{
    [ApiDescriptionSettings(Tag = "Website", Name = "WebsiteForHtml", Order = 200)]
    [Route("api/Website/[controller]")]
    [AllowAnonymous]
    [IgnoreLog]
    public class WebsiteForHtmlServe : IDynamicApiController, ITransient
    {
        private readonly ISqlSugarRepository<WebsiteProductEntity> _sqlSugarRepository;


        public WebsiteForHtmlServe(ISqlSugarRepository<WebsiteProductEntity> sqlSugarRepository)
        {
            _sqlSugarRepository = sqlSugarRepository;
        }

        [HttpGet("testGet")]
        public async Task<Object> GetTest()
        {
            return new
            {
                res = 1
            };
        }

        [HttpGet("GetNewsList")]
        public async Task<dynamic> GetNewsList(int currentPage, int pageSize, string cageorygId, int? isOnHome)
        {
            var result = await _sqlSugarRepository.Context.Queryable<WebsiteNewsEntity>()
            .OrderBy(x => x.SortCode)
            .WhereIF(!string.IsNullOrEmpty(cageorygId), x => x.CategoryId == cageorygId)
            .WhereIF(isOnHome != null, x => x.IsOnHome == isOnHome)
            .Select(it => new GetNewsListOutDto
            {
                Id = it.Id,
                Name = it.Name,
                Image = it.Image,
                PublishTime = it.PublishTime == null ? null : it.PublishTime.Value.ToString("yyyy-MM-dd HH:mm:ss"),
            }).ToPagedListAsync(currentPage, pageSize);
            result.list.ToList().ForEach(x =>
            {
                if (!string.IsNullOrEmpty(x.Image))
                {
                    try
                    {
                        var imageJson = x.Image.ToObject<List<JObject>>();
                        if (imageJson.Any())
                        {
                            x.Image = "http://47.116.26.63" + imageJson[0]["url"].ToString();
                        }
                    }
                    catch (Exception ex)
                    {

                    }
                }

            });
            return result;
        }

        [HttpGet("GetNewsDetail")]
        public async Task<dynamic> GetNewsDetail(string id)
        {
            var result = await _sqlSugarRepository.Context.Queryable<WebsiteNewsEntity>()
            .Where(x=>x.Id==id)
            .Select(it => new 
            {
                Id = it.Id,
                Name = it.Name,
                Image = it.Image,
                RichText = it.RichText,
                PublishTime = it.PublishTime == null ? null : it.PublishTime.Value.ToString("yyyy-MM-dd HH:mm:ss"),
            }).FirstAsync();
            return result;
        }

        [HttpGet("GetNewestProducrtByProductCategoryId")]
        public async Task<dynamic> GetNewestProducrtByProductCategoryId(string id)
        {
            var result = await _sqlSugarRepository.Context.Queryable<WebsiteProductEntity>()
            .Where(x => x.ProductCategoryId == id)
            .OrderByDescending(x => x.SortCode)
            .FirstAsync();
            return result;
        }

        [HttpGet("GetProducrtDetailById")]
        public async Task<dynamic> GetProducrtDetailById(string id)
        {
            var result = await _sqlSugarRepository.Context.Queryable<WebsiteProductEntity>()
            .Where(x => x.Id == id)
            .FirstAsync();
            return result;
        }
    }
}
